package fisio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import fisio.connection.ConnectionManager;
import fisio.global.Util;
import fisio.model.Atendimento;
import fisio.model.Fisioterapeuta;
import fisio.model.Paciente;
import fisio.model.Prontuario;

public class AtendimentoDAO {

	private static final String SQL_INSERT_ATENDIMENTO = "INSERT INTO atendimento (id_f, id_p) VALUES (?, ?)";
	private static final String SQL_INSERT_PRONTUARIO = "INSERT INTO prontuario (id_a, tipo, data_horario, conteudo, anamnese) VALUES (?, ?, ?, ?, ?)";
	private static final String SQL_GET_ATENDIMENTOS_BY_ID_F = "SELECT * FROM atendimento WHERE id_f = ?";
	private static final String SQL_GET_ATENDIMENTOS_BY_ID_P = "SELECT * FROM atendimento WHERE id_p = ?";
	private static final String SQL_GET_PRONTUARIOS_BY_ID = "SELECT * FROM prontuario WHERE id_a = ?";
	
	public static Vector<Atendimento> getAtendimentosFisioterapeuta(Fisioterapeuta fisioterapeuta) {

		Connection conn = ConnectionManager.getConnection();
		Vector<Atendimento> atendimentos = new Vector<Atendimento>();

		try {

			PreparedStatement stmt = conn.prepareStatement(SQL_GET_ATENDIMENTOS_BY_ID_F);
			stmt.setInt(1, fisioterapeuta.getId());
			
			ResultSet rs = stmt.executeQuery();
			Atendimento temp;
			while(rs.next()) {
				temp = new Atendimento(
								rs.getInt(1),
								rs.getInt(2),
								rs.getInt(3),
								new Vector<Prontuario>()
						);
				atendimentos.add(temp);
			}

			rs.close();
			stmt.close();
			conn.close();

		} catch(SQLException e) {
			System.out.println("\nERRO na consulta: "+e.getMessage());
		}

		return atendimentos;
	}
	
	public static Vector<Atendimento> getAtendimentosPaciente(Paciente paciente) {

		Connection conn = ConnectionManager.getConnection();
		Vector<Atendimento> atendimentos = new Vector<Atendimento>();

		try {

			PreparedStatement stmt = conn.prepareStatement(SQL_GET_ATENDIMENTOS_BY_ID_P);
			stmt.setInt(1, paciente.getId());
			
			ResultSet rs = stmt.executeQuery();
			Atendimento temp;
			while(rs.next()) {
				temp = new Atendimento(
								rs.getInt(1),
								rs.getInt(2),
								rs.getInt(3),
								new Vector<Prontuario>()
						);
				atendimentos.add(temp);
			}

			rs.close();
			stmt.close();
			conn.close();

		} catch(SQLException e) {
			System.out.println("\nERRO na consulta: "+e.getMessage());
		}

		return atendimentos;
	}
	
	public static Atendimento insertAtendimento(Atendimento atendimento) {
		
		Connection conn = ConnectionManager.getConnection();

		int id_a = -1;
		try {

			PreparedStatement stmt = conn.prepareStatement(SQL_INSERT_ATENDIMENTO, Statement.RETURN_GENERATED_KEYS);
			stmt.setInt(1, atendimento.getIdF());
			stmt.setInt(2, atendimento.getIdP());
			
			stmt.executeUpdate();
			
			ResultSet rs = stmt.getGeneratedKeys();
			if(rs.next())
				id_a = rs.getInt(1);
			rs.close();

			stmt.close();
			conn.close();

		} catch(SQLException e) {
			System.out.println("\nERRO na insercao: "+e.getMessage());
		}
		atendimento.setId(id_a);
		return atendimento;
	}

	public static Prontuario insertProntuario(Atendimento atendimento, Prontuario prontuario) {
		
		Connection conn = ConnectionManager.getConnection();

		int id_pr = -1;
		try {

			PreparedStatement stmt = conn.prepareStatement(SQL_INSERT_PRONTUARIO, Statement.RETURN_GENERATED_KEYS);
			stmt.setInt(1, atendimento.getId());
			stmt.setInt(2, prontuario.getTipo());
			stmt.setDate(3, Util.convertDateUtilToSql(prontuario.getDataReg()));
			stmt.setString(4, prontuario.getConteudo());
			stmt.setString(5, prontuario.getAnamnese());
			
			stmt.executeUpdate();
			
			ResultSet rs = stmt.getGeneratedKeys();
			if(rs.next())
				id_pr = rs.getInt(1);
			rs.close();

			stmt.close();
			conn.close();

		} catch(SQLException e) {
			System.out.println("\nERRO na insercao: "+e.getMessage());
		}
		prontuario.setId(id_pr);
		atendimento.getProntuarios().add(prontuario);
		return prontuario;
		
	}

	public static Vector<Prontuario> getProntuarios(Atendimento atendimento) {

		Connection conn = ConnectionManager.getConnection();
		Vector<Prontuario> prontuarios = new Vector<Prontuario>();

		try {

			PreparedStatement stmt = conn.prepareStatement(SQL_GET_PRONTUARIOS_BY_ID);
			stmt.setInt(1, atendimento.getId());
			
			ResultSet rs = stmt.executeQuery();
			Prontuario temp;
			while(rs.next()) {
				temp = new Prontuario(
								rs.getInt(1),
								rs.getInt(3),
								Util.convertDateSqlToUtil(rs.getDate(4)),
								rs.getString(5),
								rs.getString(6)
						);
				prontuarios.add(temp);
			}

			rs.close();
			stmt.close();
			conn.close();

		} catch(SQLException e) {
			System.out.println("\nERRO na consulta: "+e.getMessage());
		}
		atendimento.setProntuarios(prontuarios);
		return prontuarios;
	}
}